package com.itheima.service.impl;

import com.itheima.domain.User;
import com.itheima.mapper.UserMapper;
import com.itheima.service.UserService;
import com.itheima.utils.MapperUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.List;
import java.util.Map;

public class UserServiceImpl implements UserService {
    //查询所有用户信息
    public List<User> selectAll() throws Exception {
//        //1、读取mybatis核心配置文件的信息:运行环境，映射配置文件信息
//        InputStream is = Resources.getResourceAsStream("sql.xml");
//        //2、构建SqlSessionFactory对象(构建者模式)
//        SqlSessionFactory factory = new SqlSessionFactoryBuilder().build(is);
//        //3、获取SqlSession对象
//        session = factory.openSession();
        //3、获取SqlSession对象
        SqlSession session = MapperUtil.getsession();
        //4、获取Mapper代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //5、调用接口方法
        List<User> list = mapper.selectAll();
        //释放资源
        MapperUtil.close(session);
//        //6、事务提交(查询事务用事务，没事务就算了)
//        session.commit();
//        //7、释放资源
//        session.close();
//        is.close();
        return list;
    }

    //根据id查询
    public User selectById(Integer uid) throws Exception {
        //3、获取SqlSession对象
        SqlSession session = MapperUtil.getsession();
        //4、获取Mapper代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //5、调用接口方法
        User user = mapper.selectById(uid);
        //释放资源
        MapperUtil.close(session);
        return user;
    }

    //添加用户信息
    public Integer insertUser(User user) throws Exception {
        //3、获取SqlSession对象
        SqlSession session = MapperUtil.getsession();
        //4、获取Mapper代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //5、调用接口方法
        Integer users = mapper.insertUser(user);
        //释放资源
        MapperUtil.close(session);
        return users;
    }

    //修改用户信息
    public Integer updateUser(User user) throws Exception {
        //3、获取SqlSession对象
        SqlSession session = MapperUtil.getsession();
        //4、获取Mapper代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //5、调用接口方法
        Integer users = mapper.updateUser(user);
        //释放资源
        MapperUtil.close(session);
        return users;
    }

    //删除用户信息
    public Integer deleteUser(Integer uid) throws Exception {
        //3、获取SqlSession对象
        SqlSession session = MapperUtil.getsession();
        //4、获取Mapper代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //5、调用接口方法
        Integer users = mapper.deleteUser(uid);
        //释放资源
        MapperUtil.close(session);
        return users;
    }

    //登录操作
    public User findByNameAndPwd(Map<String,String> map) throws Exception {
        //获取Sqlsession对象
        SqlSession session = MapperUtil.getsession();
        //获取Mapper代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用接口方法
        User user = mapper.findByNameAndPwd(map);
        //释放资源
        MapperUtil.close(session);
        return user;
    }

    //统计男女人数
    public List findSex() throws Exception {
        //获取Sqlsession对象
        SqlSession session = MapperUtil.getsession();
        //获取Mapper代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用接口方法
        List list = mapper.findSex();
        //释放资源
        MapperUtil.close(session);
        return list;
    }

    //if判断查询用户信息
    public List findCondition() throws Exception {
        //获取Sqlsession对象
        SqlSession session = MapperUtil.getsession();
        //获取Mapper代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用接口方法
        List<User> list = mapper.findCondition();
        //释放资源
        MapperUtil.close(session);
        return list;
    }

    public List<User> findCondition1(List list) throws Exception {
        System.out.println(list+"www");
        //获取Sqlsession对象
        SqlSession session = MapperUtil.getsession();
        //获取Mapper代理对象
        UserMapper mapper = session.getMapper(UserMapper.class);
        //调用接口方法
        List<User> list1 = mapper.findCondition1(list);
        //释放资源
        MapperUtil.close(session);
        return list1;
    }
}
